package com.jingyanzi.privilege.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.jingyanzi.privilege.service.IBtnService;
import com.jingyanzi.privilege.service.IDataAccessService;
import com.jingyanzi.privilege.service.IFieldService;
import com.jingyanzi.privilege.service.IJobService;
import com.jingyanzi.privilege.service.IMenuService;
import com.jingyanzi.privilege.service.IOrgService;
import com.jingyanzi.privilege.service.IResourceService;
import com.jingyanzi.privilege.service.IRoleService;
import com.jingyanzi.privilege.service.ITableService;
import com.jingyanzi.privilege.service.IEmployeeService;



public class PrivilegeFactory {

	private static AtomicBoolean isInit = new AtomicBoolean(false);
	
	private static ApplicationContext context = null;
	
	private static AtomicBoolean isTest = new AtomicBoolean(false);
	
	public static void initCofing() {
		Properties properties = new Properties();
		InputStream in = PrivilegeFactory.class.getClassLoader().getResourceAsStream("application.properties");
		try {
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String value = properties.getProperty("run.test");
		if(value.equals("on")) {
			isTest.set(true);
		}
		if (!isInit.get()) {
			if(isTest.get()) {
				context = new FileSystemXmlApplicationContext(
						"classpath:privilege-test-application.xml");
				System.out.println("init test application.xml");
			} else {
				context = new FileSystemXmlApplicationContext(
						"classpath:privilege-application.xml");
				System.out.println("init application.xml");
			}
			isInit.set(true);
		}	
	}
	
	public static IBtnService getBtnService() {
		initCofing();
		IBtnService btnService = (IBtnService) context.getBean("btnService");
		return btnService;
	}
	
	public static IMenuService getMenuService() {
		initCofing();
		IMenuService menuService = (IMenuService) context.getBean("menuService");
		return menuService;
	}
	
	public static IRoleService getRoleService() {
		initCofing();	
		IRoleService roleService = (IRoleService) context.getBean("roleService");
		return roleService;
	}
	
	public static IOrgService getOrgService() {
		initCofing();	
		IOrgService orgService = (IOrgService) context.getBean("orgService");
		return orgService;
	}
	
	public static ITableService getTableService() {
		initCofing();	
		ITableService tableService = (ITableService) context.getBean("tableService");
		return tableService;
	}
	
	public static IFieldService getFiledService() {
		initCofing();
		IFieldService fieldService = (IFieldService) context.getBean("fieldService");
		return fieldService;
	}
	
	public static IEmployeeService getEmployeeService() {
		initCofing();	
		IEmployeeService userService = (IEmployeeService) context.getBean("employeeService");
		return userService;
	}
	
	public static IJobService getJobService() {
		initCofing();
		IJobService jobService = (IJobService) context.getBean("jobService");
		return jobService;
	}

	public static IResourceService getResourceService(){
		initCofing();
		IResourceService resourceService = (IResourceService) context.getBean("resourceService");
		return resourceService;
	}
	
	public static IDataAccessService getDataAccessService(){
		initCofing();
		IDataAccessService dataAccessService = (IDataAccessService) context.getBean("dataAccessService");
		return dataAccessService;
	}
}
